package com.作业.Day19;


import lombok.Data;

/**
 * 编写两个线程,一个线程打印1-52的整数，另一个线程打印字母A-Z。
 * 打印顺序为12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印，并且每打印两个整数后，
 * 打印一个字母，交替循环打印，直到打印到整数52和字母Z结束。
 *
 *
 */
public class Test2 {
    public static void main(String[] args) {
        Printer printer = new Printer();
        new NumberPrinter(printer).start();
        new LetterPrinter(printer).start();
    }
}
@Data
class Printer {
   private int index=1;

  synchronized void print(int num){
      if(index%3==0){
          try {
              wait();
          } catch (InterruptedException e) {
              throw new RuntimeException(e);
          }
      }
      else{
          System.out.print(num);
          index++;
          notifyAll();
      }
    }
    synchronized void print(char ch){
      if(index%3!=0){
          try {
              wait();
          } catch (InterruptedException e) {
              throw new RuntimeException(e);
          }
      }
      else {
          System.out.print(ch);
          index++;
          notifyAll();
      }
    }
}
class NumberPrinter extends Thread{
    private Printer p;
    private int num=1;
    public NumberPrinter(Printer p) {
        this.p = p;
    }

    @Override
    public void run() {
          synchronized (p){
              while (num<=52){
                  p.print(num);
                  if(p.getIndex()%3!=0)
                  num++;
          }


      }
    }
}
class LetterPrinter extends Thread{
    private Printer p;
    private char ch='A';
    public LetterPrinter(Printer p) {
        this.p = p;
    }
    @Override
    public void run() {
         synchronized (p){
             while (ch<='Z') {
                 p.print(ch);
                 if(p.getIndex()%3!=0)
                     ch++;
             }
         }
        }

    }





///**
// * 执行任务
// */
//class MyThread1 implements Runnable{
//    int flag=1;
//    int num=1;
//    char ch='A';
//    @Override
//    public void run() {
//        while(true){
//      synchronized ( this){
//          if(flag==1&&num<=52){
//              System.out.print(num++);
//              System.out.print(num++);
//              flag=2;
//          }
//          else if( (flag==2) && (ch<='Z')){
//              System.out.print( ch);
//              ch++;
//              flag=1;
//          }
//          else if(num>52&&ch>'Z'){
//              System.out.println("执行完毕");
//              break;
//          }
//      }
//
//        }
//    }
//}